package com.shanzmoo.db.store.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.base.exception.StoreException;
import com.shanzmoo.base.util.ObjectUtils;
import com.shanzmoo.db.store.entity.StoreInfoEntity;
import com.shanzmoo.db.store.entity.vo.StoreInfoVO;
import com.shanzmoo.db.store.mapper.StoreInfoMapper;
import com.shanzmoo.db.store.entity.query.StoreInfoQuery;
import com.shanzmoo.db.store.service.IStoreInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shanzmoo.db.store.entity.vo.StoreTreeSelectVO;
import com.shanzmoo.db.store.entity.vo.StoreTreeVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库库位 服务实现类
 * </p>
 *
 * @author xueqz
 * @since 2021-03-03
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfoEntity> implements IStoreInfoService {

    @Override
    public List<StoreTreeVO> storeTreeList() {
        List<StoreInfoEntity> storeInfoEntityList = baseMapper.selectList(new QueryWrapper<StoreInfoEntity>()
//                .like(StrUtil.isNotBlank(query.getName()), "name", query.getName())
//                .eq(StrUtil.isNotBlank(query.getCode()), "code", query.getCode())
                .eq("deleted", false)
        );
        return getStoreTree(storeInfoEntityList, 0,"顶级仓库");
    }

    @Override
    public List<StoreTreeSelectVO> storeTreeSelectList() {
        List<StoreInfoEntity> storeInfoEntityList = baseMapper.selectList(new QueryWrapper<StoreInfoEntity>()
//                .like(StrUtil.isNotBlank(query.getName()), "name", query.getName())
//                .eq(StrUtil.isNotBlank(query.getCode()), "code", query.getCode())
                        .eq("deleted", false)
        );
        return getStoreSelectTree(storeInfoEntityList, 0,"顶级仓库");
    }

    @Override
    public void deleteById(Integer id) {
        List<StoreInfoEntity> storeInfoEntityList = baseMapper.selectList(new QueryWrapper<StoreInfoEntity>()
                .eq("parent_id", id)
                .eq("deleted", false));
        if(CollUtil.isNotEmpty(storeInfoEntityList)){
            throw new StoreException("有子库存不允许删除");
        }else if(false){
            //todo 仓库有商品时不允许删除
            throw new StoreException("仓库有商品时不允许删除");
        }else{
            StoreInfoEntity entity = new StoreInfoEntity();
            entity.setId(id);
            entity.setDeleted(true);
            this.updateById(entity);
        }
    }

    @Override
    public List<StoreInfoVO> getFirstList() {
        List<StoreInfoEntity> list = this.lambdaQuery()
                .eq(StoreInfoEntity::getDeleted, false)
                .eq(StoreInfoEntity::getLevel, 1)
                .list();
        return list.stream().map(StoreInfoVO::create).collect(Collectors.toList());
    }

    private List<StoreTreeVO> getStoreTree(List<StoreInfoEntity> storeInfoEntityList, Integer parentId, String parentName) {
        List<StoreTreeVO> storeTreeVOList = new ArrayList<>();
        for (StoreInfoEntity storeInfoEntity : storeInfoEntityList) {
            if(parentId.equals(storeInfoEntity.getParentId())){
                StoreTreeVO storeTreeVO = ObjectUtils.convert(storeInfoEntity, StoreTreeVO.class);
                storeTreeVO.setParentName(parentName);
                storeTreeVOList.add(storeTreeVO);
            }
        }
        for (StoreTreeVO storeTreeVO : storeTreeVOList) {
            List<StoreTreeVO> storeTree = getStoreTree(storeInfoEntityList, storeTreeVO.getId(), storeTreeVO.getName());
            if(storeTree != null && storeTree.size() > 0){
                storeTreeVO.setChildList(storeTree);
            }
        }
        return storeTreeVOList;
    }

    private List<StoreTreeSelectVO> getStoreSelectTree(List<StoreInfoEntity> storeInfoEntityList, Integer parentId, String parentName) {
        List<StoreTreeSelectVO> storeTreeSelectVOList = new ArrayList<>();
        for (StoreInfoEntity storeInfoEntity : storeInfoEntityList) {
            if(parentId.equals(storeInfoEntity.getParentId())){
                StoreTreeSelectVO storeTreeSelectVO = ObjectUtils.convert(storeInfoEntity, StoreTreeSelectVO.class);
                storeTreeSelectVO.setParentName(parentName);
                storeTreeSelectVOList.add(storeTreeSelectVO);
            }
        }
        for (StoreTreeSelectVO storeTreeSelectVO : storeTreeSelectVOList) {
            List<StoreTreeSelectVO> list = getStoreSelectTree(storeInfoEntityList, storeTreeSelectVO.getId(), storeTreeSelectVO.getName());
            if(list != null && list.size() > 0){
                storeTreeSelectVO.setChildList(list);
            }
        }
        return storeTreeSelectVOList;
    }

}
